/**
 * 
 */
package dms.service.imp;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dms.dto.CampusDTO;
import dms.model.Campus;
import dms.model.util.PageIterator;
import dms.repository.CampusRepository;
import dms.service.CampusService;

/**
 * @author 李健
 *
 */
public class CampusServiceImp implements CampusService {

	private CampusRepository campusRepository = null;
	
	private static final Log log = LogFactory.getLog(CampusServiceImp.class);
	
	/**
	 * 
	 */
	public CampusServiceImp() {
		super();
	}

	/**
	 * @param campusRepository
	 */
	public CampusServiceImp(CampusRepository campusRepository) {
		super();
		this.campusRepository = campusRepository;
	}

	/**
	 * @return the campusRepository
	 */
	public CampusRepository getCampusRepository() {
		return campusRepository;
	}

	/**
	 * @param campusRepository the campusRepository to set
	 */
	public void setCampusRepository(CampusRepository campusRepository) {
		this.campusRepository = campusRepository;
	}

	@Override
	public Campus createCampus(CampusDTO campusInfo) {
		Campus campus = new Campus();
		
		try {
			BeanUtils.copyProperties(campus, campusInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}
		return campusRepository.saveCampus(campus);
		
	}

	@Override
	public void deleteCampusById(Long id) {
		
		this.campusRepository.deleteCampusById(id);
		
	}
	
	public void deleteCampuses(List<Long> ids) {
		this.campusRepository.deleteCampuses(ids);
	}

	@Override
	public PageIterator<Campus> findAll(int start, int count) {
		return this.campusRepository.findAll(start, count);
	}

	@Override
	public Campus findById(Long id) {
		return this.campusRepository.findById(id);
	}

	@Override
	public Campus updateCampus(CampusDTO campusInfo) {
		Campus campus = new Campus();
		
		try {
			BeanUtils.copyProperties(campus, campusInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}
		
		return this.campusRepository.updateCampus(campus);
	}

}
